/**
 *
 * @author spider
 */
package br.unb.plugins.geral.view;

import java.util.ArrayList;
import javax.swing.JComboBox;

import br.unb.plugins.geral.Action.XmlFrameAction;
import br.unb.plugins.geral.model.XmlComboItemModel;

public class XmlComboBox extends JComboBox {

    private String xmlFile;
    private XmlFrame xmlFrame;
    private ArrayList<XmlComboItemModel> comboList; //this.comboList != combobox list, first element is empty on the menu
    public static final String defaultComboElement = "Distribution";

    public XmlComboBox(String xmlFile) {
        this(xmlFile, null);
    }

    public XmlComboBox(String xmlFile, XmlFrameAction act) {
        super();
        addItem(defaultComboElement);
        this.xmlFile = xmlFile;
        this.xmlFrame = new XmlFrame();
        this.comboList = XmlFactory.getComboBoxListFromXml(xmlFile);
        if (comboList != null) {
            for (XmlComboItemModel item : comboList) {
                addItem(item.getName());
            }
            if (act != null) {
                addActionListener(act);//not currently used
            }
        } else {
            //Console.println("empty list or invalid xml file");
        }
    }

    public String getXmlFile() {
        return xmlFile;
    }

    public XmlComboItemModel getSelectedItemData() {
        int index = this.getSelectedIndex();
        if (index > 0) { //first element is empty, has the defaultComboElement string
            XmlComboItemModel item = (XmlComboItemModel) this.comboList.get(index - 1);
            return item;
        }
        return null;
    }

    public XmlFrame getFrame() {
        xmlFrame.init(this.getSelectedItemData());
        return xmlFrame;
    }
}
